﻿using System.Data.Objects.SqlClient;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.InfoCollect;

namespace ZBJF.Warranty.BLL
{
    public partial class CollectTypeCollectItemMappingsService
    {
        public CollectTypeCollectItemMappings LoadByID(int id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }

        public bool IsExist(byte collectWithType, int collectItemID, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<CollectTypeCollectItemMappings>();
            if (collectWithType > 0)
            {
                expressionWhere = expressionWhere.And(o => o.CollectWithType == collectWithType);
            }
            if (collectItemID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.CollectItemID == collectItemID);
            }
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID != id);
            }
            return LoadEntities(expressionWhere).Any();
        }

        public bool Delete(int id)
        {
            var entity = LoadByID(id);
            if (entity != null)
            {
                return DeleteEntities(entity);
            }
            return false;
        }
        public IQueryable<CollectTypeCollectItemMappings> GetPagingList(CollectTypeCollectItemMappingsModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<CollectTypeCollectItemMappings>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                expressionWhere =
                    expressionWhere.And(
                        o =>
                            (o.InsuranceProductsInfoCollectItem.InfoCollectItemName + SqlFunctions.Space(1) +
                             o.InsuranceProductsInfoCollectItem.InsuranceProductsInfoCollectCategory.InfoCollectCategoryName)
                                .Contains(model.Keyword));
            }
            if (model.CollectWithType > 0)
            {
                expressionWhere = expressionWhere.And(o => o.CollectWithType == model.CollectWithType);
            }
            if (model.InfoCollectCategoryID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.InsuranceProductsInfoCollectItem.InfoCollectCategoryID == model.InfoCollectCategoryID);
            }

            return LoadPagerEntities<CollectTypeCollectItemMappings>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
    }
}